package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.MemberCardDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.model.wills.MemberCard;
import com.aidong.model.wills.MemberInfo;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"cardMake"})
public class MemberCardMakeController extends BaseController {
    @Resource
    MemberCardDao memberCardDao;
    @Resource
    MemberInfoDao memberInfoDao;

    public MemberCardMakeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("card/make/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        if(!this.chkNull(status) && status.intValue() == 3) {
            cd.in("c.status", new Object[]{Integer.valueOf(3)});
        } else {
            cd.eq("c.status", Integer.valueOf(2));
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("useDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("useDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.memberCardDao.queryPageSaleList(pg, cd);
        ModelAndView mv = new ModelAndView("card/make/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("cardCode") String cardCode) {
        MemberCard card = this.memberCardDao.getById(cardCode);
        if(card != null && card.getStatus().intValue() >= 2) {
            MemberInfo member = this.memberInfoDao.getById(card.getMemberCode());
            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                String idNumber = member.getIdNumber();
                if(idNumber != null && idNumber.length() > 4) {
                    idNumber = idNumber.substring(0, idNumber.length() - 4) + "****";
                }

                StringBuilder sb = new StringBuilder();
                sb.append("{cardCode:\"").append(card.getCardCode()).append("\"").append(",memberCode:\"").append(member.getMemberCode()).append("\"").append(",memberName:\"").append(member.getChName()).append("\"").append(",gender:\"").append(member.getGender()).append("\"").append(",genderName:\"").append(this.cache.getValue("gender", member.getGender())).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idNumber:\"").append(idNumber).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",memberPhoto:\"").append(this.getSetting("pic.path") + member.getMemberPhoto()).append("\"").append("}");
                ModelAndView mv = new ModelAndView("print/print");
                mv.addObject("data", sb.toString());
                mv.addObject("_picPath", this.getSetting("pic.path"));
                mv.addObject("title", "会员卡打印");
                mv.addObject("ptjs", "card");
                return mv;
            }
        } else {
            return this.showAlert("卡号错误");
        }
    }

    @RequestMapping({"_print"})
    @ResponseBody
    public String _print(@RequestParam("cardCode") String cardCode) {
        MemberCard card = this.memberCardDao.getById(cardCode);
        if(card == null) {
            return this.getResult(false, "卡号错误!");
        } else {
            this.addUserLog("打印会员卡", "卡号:" + cardCode);
            return this.getResult(true, "操作成功");
        }
    }

    @RequestMapping({"_make"})
    @ResponseBody
    public String _make(@RequestParam("cardCode") String cardCode) {
        MemberCard card = this.memberCardDao.getById(cardCode);
        if(card == null) {
            return this.getResult(false, "没有这个卡号");
        } else if(card.getStatus().intValue() > 2) {
            return this.getResult(false, "已经制卡");
        } else if(card.getStatus().intValue() < 2) {
            return this.getResult(false, "不能制卡");
        } else {
            card.setStatus(Integer.valueOf(3));
            card.setMakeCardUser(this.getLoginUserCode());
            card.setMakeCardDate(new Date());

            try {
                this.memberCardDao.updateMakeCard(card);
                this.addUserLog("保存制卡", "卡号:" + card.getCardCode() + ",合同号:" + card.getContractCode() + ",会员号:" + card.getMemberCode());
            } catch (Exception var4) {
                this.log.error("card make", var4);
                return this.getResult(false, "保存制卡发生错误");
            }

            return this.getResult(true, "操作成功");
        }
    }
}